/*
 * atomic.h
 *
 *  (c) Normen Seemann
 *  Created on: May 2, 2009
 *
 */

#ifndef ATOMIC_H_
#define ATOMIC_H_

#include <boost/interprocess/detail/atomic.hpp>

#include <boost/thread/mutex.hpp>
#include <boost/thread/locks.hpp>
#include <boost/thread/condition_variable.hpp>

namespace crossEyed
{
namespace core
{

using namespace boost::interprocess::detail ;

class AtomicUInt
{
private:
   boost::uint32_t value ;

public:

   AtomicUInt( unsigned int newValue )
   {
      poke( newValue ) ;
   }

   unsigned int peek()
   {
      return (unsigned int)atomic_read32(&value);
   }

   void poke( unsigned int newValue )
   {
      atomic_write32( &value, (boost::uint32_t) newValue );
   }

   AtomicUInt & operator ++ (int)
   {
      atomic_inc32 ( &value ) ;
      return *this ;
   }

   AtomicUInt & operator -- (int)
   {
      atomic_dec32 ( &value ) ;
      return *this ;
   }


   inline unsigned int cas( unsigned int with, unsigned int cmp )
   {
      return atomic_cas32(&value,
                          (boost::uint32_t) with,
                          (boost::uint32_t) cmp) ;
   }
   //
   // operator to cast to an int
   //
   operator unsigned int()
   {
      return peek();
   }

};



}}
#endif /* ATOMIC_H_ */
